package com.zs.toolbox.web2j.system.core.controller.common;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zs.toolbox.web2j.common.request.WtPageBodyModel;
import com.zs.toolbox.web2j.common.request.WtPageQueryModel;
import com.zs.toolbox.web2j.common.request.WtPrimaryKeyReqeust;
import com.zs.toolbox.web2j.common.response.WtBaseResponse;
import com.zs.toolbox.web2j.common.web.WtAuthContext;
import com.zs.toolbox.web2j.common.web.WtBaseController;
import com.zs.toolbox.web2j.system.core.entity.WtUserMessageEntity;
import com.zs.toolbox.web2j.system.core.request.WtBatchPrimaryKeyRequest;
import com.zs.toolbox.web2j.system.core.request.WtSaveUserMessageRequest;
import com.zs.toolbox.web2j.system.core.service.WtUserMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import static com.zs.toolbox.web2j.common.exception.WtResponses.SUCCESS;

/**
 * <p>
 * 用户-消息表 前端控制器
 * </p>
 *
 * @author eric.zhang
 * @since 2018-10-10
 */
@RestController
@RequestMapping("/api/v1/user/message")
@Api("用户消息管理")
public class WtCommonUserMessageController extends WtBaseController {

    @Autowired
    WtUserMessageService wtUserMessageService;

    @ApiOperation("用户发送消息")
    @RequestMapping(value = "/save", method = {RequestMethod.POST, RequestMethod.GET})
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    WtBaseResponse<String> saveUserMessage(WtSaveUserMessageRequest request) {
        this.volidate(request);
        wtUserMessageService.saveMessages(request);
        return SUCCESS;
    }

    @ApiOperation("设置批量读消息")
    @RequestMapping(value = "/setReaded", method = {RequestMethod.POST, RequestMethod.GET})
    @PreAuthorize("isAuthenticated()")
    WtBaseResponse<WtUserMessageEntity> getUserMessageDetail(WtBatchPrimaryKeyRequest<Integer> reqeust) {
        this.volidate(reqeust);
        wtUserMessageService.setRead(reqeust);
        return SUCCESS;
    }

    @ApiOperation("获得未读消息数")
    @RequestMapping(value = "/getUnReadCount", method = {RequestMethod.POST, RequestMethod.GET})
    @PreAuthorize("isAuthenticated()")
    WtBaseResponse<Integer> getUnReadMessageCount() {
        return new WtBaseResponse<>(wtUserMessageService.count(new LambdaQueryWrapper<WtUserMessageEntity>()
                .eq(WtUserMessageEntity::getReaded, false)
                .eq(WtUserMessageEntity::getUserId, WtAuthContext.getUserId())));
    }

    @ApiOperation("获得用户通知详情")
    @RequestMapping(value = "/detail", method = {RequestMethod.POST, RequestMethod.GET})
    @PreAuthorize("isAuthenticated()")
    WtBaseResponse<WtUserMessageEntity> getUserMessageDetail(WtPrimaryKeyReqeust<Integer> reqeust) {
        this.volidate(reqeust);

        WtUserMessageEntity wtUserMessageEntity = wtUserMessageService.getDetailAndSetReaded(reqeust);

        return new WtBaseResponse<WtUserMessageEntity>(wtUserMessageEntity);
    }

    @ApiOperation("查看个人用户通知列表")
    @RequestMapping(value = "/getUserMessage", method = {RequestMethod.POST, RequestMethod.GET})
    @PreAuthorize("isAuthenticated()")
    WtBaseResponse<WtPageBodyModel<WtUserMessageEntity>> getUserMessages(WtPageQueryModel request) {

        IPage<WtUserMessageEntity> page = wtUserMessageService.page(new Page(request.getPage(),
                        request.getRows())
                , new LambdaQueryWrapper<WtUserMessageEntity>()
                        .eq(WtUserMessageEntity::getUserId, WtAuthContext.getUserId())
                        .orderByAsc(WtUserMessageEntity::getId));

        return new WtBaseResponse<WtPageBodyModel<WtUserMessageEntity>>(
                new WtPageBodyModel<WtUserMessageEntity>(page.getTotal(), page.getCurrent(), page.getRecords())
        );
    }
}
